import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:signage/l10n/l10n.dart';
import 'package:signage/widgets/custom_appbar.dart';
import 'package:signage/widgets/device_list_item.dart';

import '../controllers/ota_controller.dart';

class OtaView extends GetView<OtaController> {
  const OtaView({super.key});
  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          backgroundColor: Color(0xFFFAFAFA),
          appBar: CustomAppBar(
            title: AppLocalizations.of(context)!.translate('firmware_upgrade'),
            rightButtons: [
              GestureDetector(
                onTap: () {
                  controller.updateAllDevices();
                },
                child: Row(
                  children: [
                    Image.asset('assets/images/Frame@2x (7).png', width: 20.w),
                    4.horizontalSpace,
                    Text(
                      AppLocalizations.of(context)!.translate('update_all'),
                      style: TextStyle(
                        fontSize: 15.sp,
                        fontWeight: FontWeight.w400,
                        letterSpacing: 0,
                        color: Color.fromRGBO(51, 51, 51, 1),
                      ),
                    ),
                    10.horizontalSpace,
                  ],
                ),
              )
            ],
            backgroundColor: Colors.white,
            loading: true,
            loadingRotating: FlutterBluePlus.isScanningNow,
            onLoadingTap: () {
              controller.bluetoothManager.checkPermissionAndStartScan();
            },
          ),
          body: Container(
            margin: EdgeInsets.all(16.w),
            padding: EdgeInsets.all(16.w),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12.r),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  AppLocalizations.of(context)!.translate('upgradeable_devices'),
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                    letterSpacing: 0,
                    color: Color.fromRGBO(51, 51, 51, 1),
                  ),
                ),
                Expanded(
                    child: controller.devicesNeedUpdate.isEmpty
                        ? Center(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/images/Group 1321318735@2x.png',
                                  width: 232.w,
                                  height: 200.h,
                                ),
                                const SizedBox(height: 16),
                                Text(
                                  AppLocalizations.of(context)!.translate('no_upgradeable_devices'),
                                  style: TextStyle(
                                    fontSize: 15.sp,
                                    color: Colors.black54,
                                  ),
                                ),
                              ],
                            ),
                          )
                        : ListView.builder(
                            shrinkWrap: true,
                            padding: EdgeInsets.zero,
                            itemCount: controller.devicesNeedUpdate.length,
                            itemBuilder: (context, index) {
                              final cachedInfo =
                                  controller.devicesNeedUpdate[index];
                              return Column(
                                children: [
                                  16.verticalSpace,
                                  DeviceListItem(
                                    cachedInfo: cachedInfo,
                                    connectionStateStream:
                                        controller.getDeviceConnectionState(
                                            cachedInfo.id),
                                    onRemove: (String) {}, // 移除删除功能
                                    onTap: () => {},
                                    showUpdateButton: true,
                                    isView: true,
                                    dfuUpdate:
                                        controller.updateStatusList[index],
                                    onUpdate: () => controller.startOtaUpdate(
                                        controller.updateStatusList[
                                            index]), // 修改点击事件为检查更新
                                  )
                                ],
                              );
                            },
                          ))
              ],
            ),
          ),
        ));
  }
}
